package logic

import (
	"ViewSrv/global/model"
	"context"
	"time"

	"ViewSrv/internal/svc"
	"ViewSrv/view"

	"github.com/zeromicro/go-zero/core/logx"
)

type ViewCountLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewViewCountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ViewCountLogic {
	return &ViewCountLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ViewCountLogic) ViewCount(in *view.ViewCountRequest) (*view.ViewCountResponse, error) {
	// todo: add your logic here and delete this line
	Now := time.Now().AddDate(0, 0, 0)
	day := time.Now().AddDate(0, 0, -1)
	week := time.Now().AddDate(0, 0, -7)
	month := time.Now().AddDate(0, -1, 0)
	var Day, Week, Month int64
	model.DB.Debug().Model(&model.VideoLogLen{}).Select("sum(len)").Group("user_id").Where("updated_at >= ? and updated_at<=?", day, Now).Scan(&Day)
	model.DB.Debug().Model(&model.VideoLogLen{}).Select("sum(len)").Group("user_id").Where("updated_at >= ? and updated_at<=?", week, Now).Scan(&Week)
	model.DB.Debug().Model(&model.VideoLogLen{}).Select("sum(len)").Group("user_id").Where("updated_at >= ? and updated_at<=?", month, Now).Scan(&Month)
	return &view.ViewCountResponse{
		Day:   Day,
		Work:  Week,
		Month: Month,
	}, nil
}
